#!/usr/bin/python
# -*- coding: utf-8 -*-

__author__ = 'lyj'


import StringIO

from twisted.internet import reactor
from twisted.web.client import getPage
from twisted.python.util import println
from lxml import etree

def parseHtml(html):
    print 10/0
    parser = etree.HTMLParser(encoding='utf8')
    tree = etree.parse(StringIO.StringIO(html), parser)
    return tree

def extractTitle(tree):
    titleText = unicode(tree.xpath("//title/text()")[0])
    return titleText

def printError(error):
    print error

d = getPage("http://www.google.com")
d.addCallback(parseHtml)
d.addCallback(extractTitle)
d.addErrback(printError)
d.addBoth(println)


reactor.run()
    

if __name__ == '__main__':
    pass  